package com.huihui.fragment.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huihui.R;
import com.huihui.datamanager.AppManager;
import com.huihui.eventbus.SettingUserInfoSuccess;
import com.huihui.eventbus.TestEvent;
import com.huihui.fragment.BaseFragment;
import com.huihui.utils.IntentUtils;
import com.huihui.utils.LoadImageUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/8/13.
 */

public class MineFragment extends BaseFragment {

    private LinearLayout setting_userinfo;
    private CircleImageView user_head;
    private TextView user_name;
    private ImageView user_more;
    private LinearLayout msg_ll;
    private LinearLayout zan_ll;
    private LinearLayout footer_ll;
    private LinearLayout feedback_ll;
    private LinearLayout setting_ll;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine,container,false);
        initView(view);
        initData();
        initListener();
        return view;
    }

    private void initData() {
        if(AppManager.getUserInfo()!=null){
            Log.d(TAG,"用户头像地址 url - "+AppManager.getUserInfo().getHead_img());
            LoadImageUtil.displayImage(getContext(),AppManager.getUserInfo().getHead_img(),user_head);
            user_name.setText(AppManager.getUserInfo().getNick_name());
        }
    }
    private void initListener() {
        setting_userinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置个人信息
                IntentUtils.gotoMineSettingActivity(getContext());
            }
        });
        msg_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //我的消息
                IntentUtils.gotoMineMsgActivity(getContext());
            }
        });
        zan_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //我的点赞
                IntentUtils.gotoMineZanActivity(getContext());
            }
        });
        footer_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //我的足迹
                IntentUtils.gotoMinefooterActivity(getContext());
            }
        });
        feedback_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //我的反馈
                IntentUtils.gotoFeedBackActivity(getContext());
            }
        });
        setting_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //我的设置
                IntentUtils.gotoSettingActivity(getContext());
            }
        });
    }

    private void initView(View view) {
        setting_userinfo = view.findViewById(R.id.setting_userinfo);
        user_head = view.findViewById(R.id.user_head);
        user_name = view.findViewById(R.id.user_name);
        user_more = view.findViewById(R.id.user_more);
        msg_ll = view.findViewById(R.id.msg_ll);
        zan_ll = view.findViewById(R.id.zan_ll);
        footer_ll = view.findViewById(R.id.footer_ll);
        feedback_ll = view.findViewById(R.id.feedback_ll);
        setting_ll = view.findViewById(R.id.setting_ll);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(SettingUserInfoSuccess messageEvent) {
        if(messageEvent != null){
            initData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
